﻿// openvino_cpp_diagnostic.cpp
#include <openvino/openvino.hpp>
#include <iostream>
#include <filesystem>

void comprehensiveDiagnostic() {
    std::cout << "=== OpenVINO C++ Runtime Diagnostic ===" << std::endl;

    try {
        // 1. 检查 Core 创建
        std::cout << "1. Creating OpenVINO Core..." << std::endl;
        ov::Core core;
        std::cout << "   ✅ Core created successfully" << std::endl;

        // 2. 检查插件路径
        std::cout << "2. Checking plugin paths..." << std::endl;
        std::vector<std::string> plugin_paths = {
            "D:\\DSD\\openvino_toolkit_windows_2025.3.0.19807.44526285f24_x86_64\\runtime\\lib\\intel64\\Debug",
            "D:\\DSD\\openvino_toolkit_windows_2025.3.0.19807.44526285f24_x86_64\\runtime\\lib\\intel64\\Release",
            "D:\\DSD\\miniconda\\Lib\\site-packages\\openvino\\libs",
            ".\\"  // 当前目录
        };

        for (const auto& path : plugin_paths) {
            if (std::filesystem::exists(path)) {
                std::cout << "   📁 Plugin path exists: " << path << std::endl;

                // 检查插件文件
                std::vector<std::string> plugin_files = {
                    "openvino.dll",
                    "openvino_c.dll",
                    "openvino_intel_cpu_plugin.dll",
                    "openvino_intel_gpu_plugin.dll"
                };

                for (const auto& file : plugin_files) {
                    std::string full_path = path + "\\" + file;
                    if (std::filesystem::exists(full_path)) {
                        std::cout << "      ✅ Found: " << file << std::endl;
                    }
                    else {
                        std::cout << "      ❌ Missing: " << file << std::endl;
                    }
                }
            }
        }

        // 3. 检查可用设备
        std::cout << "3. Checking available devices..." << std::endl;
        std::vector<std::string> available_devices = core.get_available_devices();
        std::cout << "   Number of devices: " << available_devices.size() << std::endl;

        if (available_devices.empty()) {
            std::cout << "   ❌ No devices found in C++ runtime!" << std::endl;

            // 尝试手动加载插件
            std::cout << "4. Attempting manual plugin loading..." << std::endl;
            try {
                core.register_plugin("openvino_intel_cpu_plugin.dll", "CPU");
                std::cout << "   ✅ CPU plugin registered manually" << std::endl;
            }
            catch (const std::exception& e) {
                std::cout << "   ❌ Manual registration failed: " << e.what() << std::endl;
            }

            // 重新检查
            available_devices = core.get_available_devices();
            std::cout << "   Devices after manual registration: " << available_devices.size() << std::endl;
        }

        for (const auto& device : available_devices) {
            std::cout << "   ✅ Device: " << device << std::endl;
            try {
                auto device_name = core.get_property(device, ov::device::full_name);
                std::cout << "      Name: " << device_name << std::endl;
            }
            catch (...) {
                std::cout << "      (Could not get detailed info)" << std::endl;
            }
        }

    }
    catch (const std::exception& e) {
        std::cout << "💥 Diagnostic failed: " << e.what() << std::endl;
    }

    // 4. 检查环境变量
    std::cout << "5. Checking environment..." << std::endl;
    const char* env_vars[] = {
        "PATH",
        "OPENVINO_DIR",
        "INTEL_OPENVINO_DIR"
    };

    for (const auto& env_var : env_vars) {
        char* value = nullptr;
        size_t len = 0;
        _dupenv_s(&value, &len, env_var);
        if (value != nullptr) {
            std::cout << "   " << env_var << " = " << value << std::endl;
            free(value);
        }
        else {
            std::cout << "   " << env_var << " = (not set)" << std::endl;
        }
    }

    std::cout << "========================================" << std::endl;
}

/*
int main() {
    comprehensiveDiagnostic();
    std::cout << "Press Enter to exit...";
    std::cin.get();
    return 0;
}
*/